package com.tianpengtech.front.mobile.task;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.taskscreenshot.TaskScreenShot;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.front.mobile.common.intercepter.TokenCheckInterceptor;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.U;
import com.tianpengtech.front.mobile.common.util.UserUtil;
import com.tianpengtech.front.web.common.controller.BaseController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ggd on 2017/7/28 0028.
 */
public class MobileTaskController extends BaseController {
	private static MobileTaskService service = MobileTaskService.me;

	/**
	 * 获取任务广场任务列表
	 */
	public void index() {
		int pageNum = getParaToInt("pageNum", 1);
		int pageSize = getParaToInt("pageSize", 5);
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		R r = service.taskIndex(pageNum, pageSize, user.getLong(User.ID));
		renderJson(r);
	}

	/**
	 * 进行中和已完成任务查询
	 */
	@Clear(TokenCheckInterceptor.class)
	public void findTask() {
		int pageNum = getParaToInt("pageNum", 1);
		int pageSize = getParaToInt("pageSize", 5);
		Map<String, Object> params = new HashMap<>();
		params.put("taskName", getPara("taskName"));
		params.put("taskStatus", getPara("statue"));
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		params.put("user", user.getLong(User.ID));
		R r = service.findTask(pageNum, pageSize, params);
		renderJson(r);
	}

	/**
	 * 获取任务详情
	 */
	public void getTask() {
		String id = getPara("id");// 任务ID
		if (!U.isInteger(id)) {
			renderJson(R.fail("id格式不正确"));
			return;
		}
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		R r = service.getTask(Long.parseLong(id), user.getLong(User.ID));
		renderJson(r);
	}

	/**
	 * 接受任务
	 */
	
	@Before(Tx.class)
	public void acceptTask() {
		String id = getPara("id");// 任务ID
		if (!U.isInteger(id)) {
			renderJson(R.fail("id格式不正确"));
			return;
		}
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		R r = service.acceptTask(id, user.getLong(User.ID));
		renderJson(r);
	}

	public void canAcceptTaskNum() {
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		R r = service.canAcceptTaskNum(user.getLong(User.ID));
		renderJson(r);
	}

	public void missionCommit() {

		try {
			User user = UserUtil.getCurrentUser(getPara("token"), "ios");
			int type = getParaToInt(TaskScreenShot.TYPE, 0);
			int taskId = getParaToInt(TaskScreenShot.TASK_ID, 0);
			String screenShot = getPara("screenshot", "");
			service.missionCommit(type, taskId, user.getLong(User.ID), screenShot);
			renderJson(R.ok("截图保存成功，感谢你的参与"));
		} catch (ValidateException e) {
			renderJson(R.fail(e.getMessage()));
		}

	}

	public void missionCommitList() {
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		int taskId = getParaToInt(TaskScreenShot.TASK_ID, 0);
		renderJson(service.missionCommitList(taskId, user.getLong(User.ID)));
	}
	
	
	public void delMissionCommit(){
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		int id = getParaToInt(TaskScreenShot.ID, 0);
		service.delMissionCommit(id);
		renderJson(R.ok("操作成功"));
	}
	
	public void updateRecomandReason(){
		
		try {
			User user = UserUtil.getCurrentUser(getPara("token"), "ios");
			String reason = getPara("reason");
			String title = getPara("title");
			int taskId = getParaToInt("id");
			service.updateRecomandReason(taskId, user.getLong(User.ID), reason,title);
			renderJson(R.ok("保存推荐理由成功"));
		} catch (ValidateException e) {
			renderJson(R.fail(e.getMessage()));
		}
	}
	
	public void taskTodayNum(){
		User user = UserUtil.getCurrentUser(getPara("token"), "ios");
		R r = R.ok();
		r.p("taskTodayNum", service.taskTodayNum(user.getLong(User.ID)));
		renderJson(r);
	}
}
